import { ref, computed } from 'vue'

// 定义权限常量
export const enum Permission {
  // 平台管理权限
  VIEW_PLATFORMS = 'view_platforms',
  MANAGE_PLATFORMS = 'manage_platforms',
  
  // 用户管理权限
  VIEW_USERS = 'view_users',
  MANAGE_USERS = 'manage_users',
  
  // 消息管理权限
  VIEW_MESSAGES = 'view_messages',
  SEND_MESSAGES = 'send_messages',
  
  // 监控权限
  VIEW_MONITOR = 'view_monitor',
  
  // 设置权限
  VIEW_SETTINGS = 'view_settings',
  MANAGE_SETTINGS = 'manage_settings',
  
  // 系统权限
  MANAGE_SYSTEM = 'manage_system',
  
  // 好友标签管理权限
  VIEW_FRIEND_TAGS = 'view_friend_tags',
  MANAGE_FRIEND_TAGS = 'manage_friend_tags',
  
  // 素材库管理权限
  VIEW_MATERIAL_LIBRARY = 'view_material_library',
  MANAGE_MATERIAL_LIBRARY = 'manage_material_library'
}

// 定义角色类型
export const enum Role {
  USER = 'user',           // 普通用户
  ADMIN = 'admin',         // 管理员
  SUPER_ADMIN = 'super_admin' // 超级管理员
}

// 角色对应的权限映射
const rolePermissions = {
  [Role.USER]: [
    Permission.VIEW_PLATFORMS,
    Permission.VIEW_MESSAGES,
    Permission.SEND_MESSAGES,
    Permission.VIEW_MONITOR,
    Permission.VIEW_FRIEND_TAGS,
    Permission.VIEW_MATERIAL_LIBRARY
  ],
  [Role.ADMIN]: [
    Permission.VIEW_PLATFORMS,
    Permission.MANAGE_PLATFORMS,
    Permission.VIEW_USERS,
    Permission.VIEW_MESSAGES,
    Permission.SEND_MESSAGES,
    Permission.VIEW_MONITOR,
    Permission.VIEW_SETTINGS,
    Permission.VIEW_FRIEND_TAGS,
    Permission.MANAGE_FRIEND_TAGS,
    Permission.VIEW_MATERIAL_LIBRARY,
    Permission.MANAGE_MATERIAL_LIBRARY
  ],
  [Role.SUPER_ADMIN]: [
    Permission.VIEW_PLATFORMS,
    Permission.MANAGE_PLATFORMS,
    Permission.VIEW_USERS,
    Permission.MANAGE_USERS,
    Permission.VIEW_MESSAGES,
    Permission.SEND_MESSAGES,
    Permission.VIEW_MONITOR,
    Permission.VIEW_SETTINGS,
    Permission.MANAGE_SETTINGS,
    Permission.MANAGE_SYSTEM,
    Permission.VIEW_FRIEND_TAGS,
    Permission.MANAGE_FRIEND_TAGS,
    Permission.VIEW_MATERIAL_LIBRARY,
    Permission.MANAGE_MATERIAL_LIBRARY
  ]
}

// 当前用户角色
export const currentRole = ref<Role>(Role.USER)

// 当前用户权限
export const currentPermissions = computed(() => {
  return rolePermissions[currentRole.value] || []
})

// 检查是否有权限
export const hasPermission = (permission: Permission): boolean => {
  return currentPermissions.value.includes(permission)
}

// 检查是否有任意一个权限
export const hasAnyPermission = (permissions: Permission[]): boolean => {
  return permissions.some(permission => hasPermission(permission))
}

// 检查是否有所有权限
export const hasAllPermissions = (permissions: Permission[]): boolean => {
  return permissions.every(permission => hasPermission(permission))
}

// 检查是否为指定角色
export const hasRole = (role: Role): boolean => {
  return currentRole.value === role
}

// 检查是否为管理员或更高权限
export const isAdminOrAbove = (): boolean => {
  return currentRole.value === Role.ADMIN || currentRole.value === Role.SUPER_ADMIN
}

// 检查是否为超级管理员
export const isSuperAdmin = (): boolean => {
  return currentRole.value === Role.SUPER_ADMIN
}

// 设置当前用户角色
export const setCurrentRole = (role: Role) => {
  currentRole.value = role
} 